import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { ConsumptionBillApi } from '#/api/spa/consumption';

// import { z } from '#/adapter/form';
import { getDictOptions, getRangePickerDefaultProps } from '#/utils';
import { DICT_SPA_TYPE } from '#/utils/dictSpa';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'customerId',
      label: '顾客ID',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入顾客ID',
      },
    },
    {
      fieldName: 'teamId',
      label: '团队ID',
      component: 'Input',
      componentProps: {
        placeholder: '请输入团队ID',
      },
    },
    {
      fieldName: 'resourceUsageId',
      label: '资源使用ID',
      component: 'Input',
      componentProps: {
        placeholder: '请输入资源使用ID（手牌/房卡/工牌等泛化）',
      },
    },
    {
      fieldName: 'billNumber',
      label: '账单编号',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入账单编号',
      },
    },
    {
      fieldName: 'totalAmount',
      label: '原始总金额',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入原始总金额',
      },
    },
    {
      fieldName: 'discountAmount',
      label: '总优惠金额',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入总优惠金额',
      },
    },
    {
      fieldName: 'payableAmount',
      label: '应付金额',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入应付金额',
      },
    },
    {
      fieldName: 'paidAmount',
      label: '已支付金额',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入已支付金额',
      },
    },
    {
      fieldName: 'status',
      label: '账单状态',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(
          DICT_SPA_TYPE.Consumption_bill_status.toUpperCase(),
          'string',
        ),
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'pointsUsed',
      label: '使用积分',
      component: 'Input',
      componentProps: {
        placeholder: '请输入使用积分',
      },
    },
    {
      fieldName: 'pointsEarned',
      label: '获得积分',
      component: 'Input',
      componentProps: {
        placeholder: '请输入获得积分',
      },
    },
    {
      fieldName: 'startTime',
      label: '消费开始时间',
      component: 'DatePicker',
      componentProps: {
        showTime: true,
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
      },
    },
    {
      fieldName: 'endTime',
      label: '消费结束时间',
      component: 'DatePicker',
      componentProps: {
        showTime: true,
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
      },
    },
    {
      fieldName: 'durationMinutes',
      label: '消费时长（分钟）',
      component: 'Input',
      componentProps: {
        placeholder: '请输入消费时长（分钟）',
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Input',
      componentProps: {
        placeholder: '请输入备注',
      },
    },
    {
      fieldName: 'groupId',
      label: '所属集团ID',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入所属集团ID',
      },
    },
    {
      fieldName: 'storeId',
      label: '所属门店ID',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入所属门店ID',
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'customerId',
      label: '顾客ID',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入顾客ID',
      },
    },
    {
      fieldName: 'teamId',
      label: '团队ID',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入团队ID',
      },
    },

    {
      fieldName: 'billNumber',
      label: '账单编号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入账单编号',
      },
    },

    {
      fieldName: 'status',
      label: '账单状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(
          DICT_SPA_TYPE.Consumption_bill_status.toUpperCase(),
          'string',
        ),
        placeholder: '请选择账单状态',
      },
    },

    {
      fieldName: 'startTime',
      label: '消费开始时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
    {
      fieldName: 'endTime',
      label: '消费结束时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<ConsumptionBillApi.ConsumptionBill>['columns'] {
  return [
    {
      field: 'id',
      title: '账单ID',
      minWidth: 120,
    },
    {
      field: 'customerId',
      title: '顾客ID',
      minWidth: 120,
    },
    {
      field: 'teamId',
      title: '团队ID',
      minWidth: 120,
    },
    {
      field: 'resourceUsageId',
      title: '资源ID',
      minWidth: 120,
    },
    {
      field: 'billNumber',
      title: '账单编号',
      minWidth: 120,
    },
    {
      field: 'totalAmount',
      title: '原始总金额',
      minWidth: 120,
    },
    {
      field: 'discountAmount',
      title: '总优惠金额',
      minWidth: 120,
    },
    {
      field: 'payableAmount',
      title: '应付金额',
      minWidth: 120,
    },
    {
      field: 'paidAmount',
      title: '已支付金额',
      minWidth: 120,
    },
    {
      field: 'status',
      title: '账单状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_SPA_TYPE.Consumption_bill_status.toUpperCase() },
      },
    },
    {
      field: 'pointsUsed',
      title: '使用积分',
      minWidth: 120,
    },
    {
      field: 'pointsEarned',
      title: '获得积分',
      minWidth: 120,
    },
    {
      field: 'startTime',
      title: '消费开始时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'endTime',
      title: '消费结束时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'durationMinutes',
      title: '消费时长（分钟）',
      minWidth: 120,
    },
    {
      field: 'remark',
      title: '备注',
      minWidth: 120,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

// ==================== 子表（SPA消费明细） ====================

/** 新增/修改的表单 */
export function useConsumptionDetailFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    //  {
    //     fieldName: 'itemType',
    //     label: '消费类型（服务/商品/套餐/房间/其他）',
    //         rules: 'required',
    //         component: 'Select',
    //         componentProps: {
    //                 options: getDictOptions(DICT_SPA_TYPE.HandCardStatus, 'string'),
    //             placeholder: '请选择消费类型（服务/商品/套餐/房间/其他）',
    //         },
    // },
    {
      fieldName: 'itemId',
      label: '对应项目',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入对应项目ID（指向服务表、商品表、房型表等）',
      },
    },
    {
      fieldName: 'itemName',
      label: '消费项目',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入消费项目',
      },
    },
    {
      fieldName: 'quantity',
      label: '数量',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入数量',
      },
    },
    {
      fieldName: 'unitPrice',
      label: '单价',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入单价',
      },
    },
    {
      fieldName: 'discountAmount',
      label: '优惠金额',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入优惠金额',
      },
    },
    {
      fieldName: 'subtotalAmount',
      label: '小计金额',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入小计金额（含折扣后）',
      },
    },
    {
      fieldName: 'staffId',
      label: '服务员工',
      component: 'Input',
      componentProps: {
        placeholder: '请输入服务员工/技师ID',
      },
    },
    {
      fieldName: 'staffCommission',
      label: '员工提成金额',
      component: 'Input',
      componentProps: {
        placeholder: '请输入员工提成金额',
      },
    },
    {
      fieldName: 'resourceUsageId',
      label: '资源ID',
      component: 'Input',
      componentProps: {
        placeholder: '请输入资源使用ID（房间/手牌/座位等绑定资源）',
      },
    },
    {
      fieldName: 'startTime',
      label: '服务开始时间',
      component: 'DatePicker',
      componentProps: {
        showTime: true,
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
      },
    },
    {
      fieldName: 'endTime',
      label: '服务结束时间',
      component: 'DatePicker',
      componentProps: {
        showTime: true,
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
      },
    },
    {
      fieldName: 'durationMinutes',
      label: '服务时长',
      component: 'Input',
      componentProps: {
        placeholder: '请输入服务时长（分钟）',
      },
    },
    {
      fieldName: 'status',
      label: '消费状态',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(
          DICT_SPA_TYPE.Consumption_bill_status.toUpperCase(),
          'string',
        ),
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Input',
      componentProps: {
        placeholder: '请输入备注',
      },
    },
    {
      fieldName: 'groupId',
      label: '所属集团ID',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入所属集团ID',
      },
    },
    {
      fieldName: 'storeId',
      label: '所属门店ID',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入所属门店ID',
      },
    },
    {
      fieldName: 'registryId',
      label: '来源的流水ID',
      component: 'Input',
      componentProps: {
        placeholder: '请输入来源的流水ID',
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useConsumptionDetailGridFormSchema(): VbenFormSchema[] {
  return [
    // {
    //     fieldName: 'billId',
    //     label: '关联账单ID',
    //         component: 'Input',
    //         componentProps: {
    //             allowClear: true,
    //             placeholder: '请输入关联账单ID',
    //         },
    // },
    // {
    //     fieldName: 'itemType',
    //     label: '消费类型（服务/商品/套餐/房间/其他）',
    //         component: 'Select',
    //         componentProps: {
    //             allowClear: true,
    //                 options: getDictOptions(DICT_SPA_TYPE.HandCardStatus, 'string'),
    //             placeholder: '请选择消费类型（服务/商品/套餐/房间/其他）',
    //         },
    // },
    // {
    //     fieldName: 'itemId',
    //     label: '对应项目ID（指向服务表、商品表、房型表等）',
    //         component: 'Input',
    //         componentProps: {
    //             allowClear: true,
    //             placeholder: '请输入对应项目ID（指向服务表、商品表、房型表等）',
    //         },
    // },
    {
      fieldName: 'itemName',
      label: '消费项目',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入消费项目',
      },
    },

    {
      fieldName: 'staffId',
      label: '服务员工',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入服务员工/技师ID）',
      },
    },

    // {
    //     fieldName: 'resourceUsageId',
    //     label: '资源使用ID（房间/手牌/座位等绑定资源）',
    //         component: 'Input',
    //         componentProps: {
    //             allowClear: true,
    //             placeholder: '请输入资源使用ID（房间/手牌/座位等绑定资源）',
    //         },
    // },
    {
      fieldName: 'startTime',
      label: '服务开始时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
    {
      fieldName: 'endTime',
      label: '服务结束时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },

    {
      fieldName: 'status',
      label: '消费状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(
          DICT_SPA_TYPE.Consumption_bill_status.toUpperCase(),
          'string',
        ),
        placeholder: '请选择消费状态',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
    {
      fieldName: 'groupId',
      label: '所属集团ID',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入所属集团ID',
      },
    },
    {
      fieldName: 'storeId',
      label: '所属门店ID',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入所属门店ID',
      },
    },
    {
      fieldName: 'registryId',
      label: '来源的流水ID',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入来源的流水ID',
      },
    },
  ];
}

/** 列表的字段 */
export function useConsumptionDetailGridColumns(): VxeTableGridOptions<ConsumptionBillApi.ConsumptionDetail>['columns'] {
  return [
    {
      field: 'id',
      title: '明细ID',
      minWidth: 120,
    },
    {
      field: 'billId',
      title: '关联账单ID',
      minWidth: 120,
    },
    {
      field: 'itemType',
      title: '消费类型',
      minWidth: 120,
    },
    {
      field: 'itemId',
      title: '对应项目ID',
      minWidth: 120,
    },
    {
      field: 'itemName',
      title: '消费项目',
      minWidth: 120,
    },
    {
      field: 'quantity',
      title: '数量',
      minWidth: 120,
    },
    {
      field: 'unitPrice',
      title: '单价',
      minWidth: 120,
    },
    {
      field: 'discountAmount',
      title: '优惠金额',
      minWidth: 120,
    },
    {
      field: 'subtotalAmount',
      title: '小计金额',
      minWidth: 120,
    },
    {
      field: 'staffId',
      title: '服务员工/技师ID',
      minWidth: 120,
    },
    {
      field: 'staffCommission',
      title: '员工提成金额',
      minWidth: 120,
    },
    {
      field: 'resourceUsageId',
      title: '资源使用ID',
      minWidth: 120,
    },
    {
      field: 'startTime',
      title: '服务开始时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'endTime',
      title: '服务结束时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'durationMinutes',
      title: '服务时长（分钟）',
      minWidth: 120,
    },
    {
      field: 'status',
      title: '消费状态',
      minWidth: 120,
    },
    {
      field: 'remark',
      title: '备注',
      minWidth: 120,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'groupId',
      title: '所属集团ID',
      minWidth: 120,
    },
    {
      field: 'storeId',
      title: '所属门店ID',
      minWidth: 120,
    },
    {
      field: 'registryId',
      title: '来源的流水ID',
      minWidth: 120,
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
